package view;

import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
import java.util.*;

import model.*;
import control.*;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NewFamily extends JPanel {
  public static final String NAME = "family";
  private SnakesCRUD parent;
  
  private ArrayList<Family> families;
  
  private CFamily familyControl;
  
  public NewFamily(SnakesCRUD parent) {
    this.parent = parent;
    
    families = new ArrayList<Family>();
    familyControl = new CFamily(parent.getConnection());
    
    initComponents();
    updateTable();
  }
  
  private final void updateTable() {
    try {
      this.families = familyControl.retrieveAll();
      
      Object[] o = new Object[3];
      ((DefaultTableModel)(tbFamily.getModel())).setRowCount(0);
      
      for (int i = 0; i < families.size(); i++) {
        o[0] = families.get(i).getName();
        o[1] = families.get(i).getObservations().equals("null")?"":families.get(i).getObservations();
        o[2] = families.get(i).getDescription();
        
        ((DefaultTableModel)(tbFamily.getModel())).addRow(o);
      }
    }
    catch(Exception e) {}
  }
  
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        lblName = new javax.swing.JLabel();
        lblObs = new javax.swing.JLabel();
        {
        	jScrollPane1 = new JScrollPane();
        	{
        		txaObs = new javax.swing.JTextArea();
        		jScrollPane1.setViewportView(txaObs);
        	}
        }
        lblDesc = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
      jLabel7 = new javax.swing.JLabel();
      {
    	  jScrollPane3 = new JScrollPane();
    	  {
    		  txaDesc = new javax.swing.JTextArea();
    		  jScrollPane3.setViewportView(txaDesc);
    	  }
      }
        jLabel8 = new javax.swing.JLabel();
        GroupLayout layout = new GroupLayout((JComponent)this);
        this.setLayout(layout);
        this.setPreferredSize(new java.awt.Dimension(530, 419));
        txtName = new javax.swing.JTextField();
        btnAdd = new javax.swing.JButton();
        btnRemove = new javax.swing.JButton();
        btnModify = new javax.swing.JButton();
        btnRefresh = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbFamily = new javax.swing.JTable();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Editing FAMILY Table");

        lblName.setText("Name:");

        lblObs.setText("Observations:");

        lblDesc.setText("Description:");

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnRemove.setText("Remove");
        btnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveActionPerformed(evt);
            }
        });

        btnModify.setText("Modify");
        btnModify.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModifyActionPerformed(evt);
            }
        });

        btnRefresh.setText("Refresh");
        layout.setHorizontalGroup(layout.createSequentialGroup()
        	.addContainerGap(29, 29)
        	.addGroup(layout.createParallelGroup()
        	    .addGroup(layout.createSequentialGroup()
        	        .addGroup(layout.createParallelGroup()
        	            .addComponent(lblObs, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(btnAdd, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(lblDesc, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        	        .addGroup(layout.createParallelGroup()
        	            .addComponent(jScrollPane3, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 364, GroupLayout.PREFERRED_SIZE)
        	            .addGroup(layout.createSequentialGroup()
        	                .addGap(18)
        	                .addGroup(layout.createParallelGroup()
        	                    .addComponent(jScrollPane1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 346, GroupLayout.PREFERRED_SIZE)
        	                    .addGroup(layout.createSequentialGroup()
        	                        .addPreferredGap(jScrollPane1, jLabel5, LayoutStyle.ComponentPlacement.INDENT)
        	                        .addComponent(jLabel5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        	                        .addGap(12)
        	                        .addComponent(btnRemove, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        	                        .addGap(69)
        	                        .addComponent(btnModify, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        	                        .addGap(26)
        	                        .addComponent(jLabel8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        	                        .addGap(35)
        	                        .addComponent(btnRefresh, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))))
        	    .addGroup(layout.createSequentialGroup()
        	        .addGroup(layout.createParallelGroup()
        	            .addComponent(lblName, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jLabel6, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jLabel7, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        	        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	        .addComponent(txtName, GroupLayout.PREFERRED_SIZE, 395, GroupLayout.PREFERRED_SIZE))
        	    .addComponent(jLabel1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)
        	    .addComponent(jScrollPane2, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 453, GroupLayout.PREFERRED_SIZE))
        	.addContainerGap(48, 48));
        layout.setVerticalGroup(layout.createSequentialGroup()
        	.addContainerGap(24, 24)
        	.addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        	.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	.addComponent(jScrollPane2, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)
        	.addGap(18)
        	.addGroup(layout.createParallelGroup()
        	    .addGroup(layout.createSequentialGroup()
        	        .addComponent(jLabel8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        	        .addGap(7)
        	        .addComponent(lblName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        	    .addComponent(txtName, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        	.addGap(18)
        	.addGroup(layout.createParallelGroup()
        	    .addComponent(lblObs, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        	    .addComponent(jScrollPane1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE))
        	.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
        	.addGroup(layout.createParallelGroup()
        	    .addGroup(layout.createSequentialGroup()
        	        .addGroup(layout.createParallelGroup()
        	            .addComponent(lblDesc, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addComponent(jLabel5, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        	        .addComponent(jLabel6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        	        .addGap(21)
        	        .addComponent(jLabel7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        	    .addComponent(jScrollPane3, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE))
        	.addGap(20)
        	.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        	    .addComponent(btnAdd, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        	    .addComponent(btnRemove, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        	    .addComponent(btnModify, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        	    .addComponent(btnRefresh, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        	.addContainerGap(20, 20));
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        tbFamily.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {},
            new String [] {
                "Name", "Observations", "Description"
            }
        ) {
            Class[] types = new Class [] {
            		java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tbFamily);

    }// </editor-fold>//GEN-END:initComponents

  private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
      try {
        
    	Family family = new Family();
    	family.setName(txtName.getText());
    	family.setDescription(txaDesc.getText());
    	family.setObservations(txaObs.getText());
    	  
        familyControl.create(family);
        updateTable();
        JOptionPane.showMessageDialog(null, "Data added!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
      catch(SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid ID!", "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (ClassNotFoundException e) {}
    
  }//GEN-LAST:event_btnAddActionPerformed

  private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveActionPerformed
    if (!txtName.getText().isEmpty()) {
      try {
        Family family = new Family();
        family.setName(txtName.getText());
    
        familyControl.delete(family);
        updateTable();
        JOptionPane.showMessageDialog(null, "Data deleted!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
      catch(SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid ID!", "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (ClassNotFoundException e) {}
    }
    else {
      JOptionPane.showMessageDialog(null, "Please, check the information provided", "Snakes", JOptionPane.INFORMATION_MESSAGE);
    }
  }//GEN-LAST:event_btnRemoveActionPerformed

  private void btnModifyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModifyActionPerformed
      try 
      {
        Family family = new Family();
        family.setName(txtName.getText());
        family.setObservations(txaObs.getText());
        family.setDescription(txaDesc.getText());
    	  
        familyControl.update(family);
        updateTable();
        JOptionPane.showMessageDialog(null, "Data updated!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
      catch(SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid ID!", "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (ClassNotFoundException e) {}
  }//GEN-LAST:event_btnModifyActionPerformed

  private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
    updateTable();
  }//GEN-LAST:event_btnRefreshActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnModify;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnRemove;
    private javax.swing.JTextArea txaDesc;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextArea txaObs;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblObs;
    private javax.swing.JLabel lblDesc;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane2;
    private JScrollPane jScrollPane3;
    private JScrollPane jScrollPane1;
    private javax.swing.JTable tbFamily;
    // End of variables declaration//GEN-END:variables
}
